package cn.com.manager.dao.weibo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.DefaultSortParameters;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.stereotype.Repository;

import cn.com.manager.utility.ApplicationConstants;

@Repository(value = "userDao")
public class UserDao extends RedisBase {
	/**
	 * 获取用户主键
	 * 
	 * @return
	 */
	private Long getPrimaryKey() {
		return super.getPrimaryKey("global:userid");
	}

	/**
	 * 新用户保存
	 * 
	 * @param username
	 * @param password
	 */
	public void saveUser(String username, String password) {
		// 保存用户信息
		Long primaryKey = getPrimaryKey();
		stringRedisTemplate.opsForValue().set("user:userid:" + primaryKey + ":username", username);
		stringRedisTemplate.opsForValue().set("user:userid:" + primaryKey + ":password", password);
		// 添加username索引
		stringRedisTemplate.opsForValue().set("user:username:" + username + ":userid", String.valueOf(primaryKey));

		// 新用户添加到新用户列表
		stringRedisTemplate.opsForList().leftPush("newuserinfo", String.valueOf(primaryKey));
		Long newuser = keyLen("newuserinfo");
		if (newuser > ApplicationConstants.weibo_show_user_newtop) {
			stringRedisTemplate.opsForList().rightPop("newuserinfo");
		}
	}

	/**
	 * 获取新注册用户名
	 * 
	 * @return
	 */
	public List<String> newUserList() {
		List<byte[]> result = stringRedisTemplate.execute(new RedisCallback<List<byte[]>>() {

			@Override
			public List<byte[]> doInRedis(RedisConnection connection) throws DataAccessException {
				DefaultSortParameters sortParam = new DefaultSortParameters();
				sortParam.addGetPattern(stringRedisTemplate.getStringSerializer().serialize("user:userid:*:username"));
				sortParam.desc();
				List<byte[]> sort = connection.sort(stringRedisTemplate.getStringSerializer().serialize("newuserinfo"),
						sortParam);
				return sort;
			}
		});
		List<String> usernameList = new ArrayList<String>();
		result.forEach(bean -> {
			usernameList.add(stringRedisTemplate.getStringSerializer().deserialize(bean));
		});
		return usernameList;
	}

	/**
	 * 根据用户名获取用户信息
	 * 
	 * @param username
	 * @return
	 */
	public Map<String, Object> getUserInfo(String username) {
		Map<String, Object> userInfo = new HashMap<String, Object>();
		String userid = stringRedisTemplate.opsForValue().get("user:username:" + username + ":userid");
		if (StringUtils.isNoneBlank(userid)) {
			String password = stringRedisTemplate.opsForValue().get("user:userid:" + userid + ":password");
			userInfo.put("userId", userid);
			userInfo.put("username", username);
			userInfo.put("password", password);
		}
		return userInfo;
	}
	
	public Map<String, Object> getUserInfoById(String userid) {
		Map<String, Object> userInfo = new HashMap<String, Object>();
		if (StringUtils.isNoneBlank(userid)) {
			String password = stringRedisTemplate.opsForValue().get("user:userid:" + userid + ":password");
			String username = stringRedisTemplate.opsForValue().get("user:userid:" + userid + ":username");
			userInfo.put("userId", userid);
			userInfo.put("username", username);
			userInfo.put("password", password);
		}
		return userInfo;
	}
	
	/**
	 * 判断是否已加关注
	 * @param username
	 * @param currentUserId
	 * @return
	 */
	public boolean isFollow(String username,String currentUserId){
		String userid = stringRedisTemplate.opsForValue().get("user:username:"+username+":userid");
		boolean keyExists = keyExists("following:"+currentUserId);
		if(keyExists){			
			Boolean member = stringRedisTemplate.opsForSet().isMember("following:"+currentUserId, userid);
			return member;
		}
		return false;
	}

	/**
	 * 添加关注
	 * 
	 * @param userId
	 * @param currentUserId
	 */
	public void followUser(String userId, String currentUserId) {
		stringRedisTemplate.opsForSet().add("following:" + currentUserId, userId);
	}

	/**
	 * 取消关注
	 * @param userId
	 * @param currentUserId
	 */
	public void cancelFollow(String userId, String currentUserId) {
		stringRedisTemplate.opsForSet().remove("following:" + currentUserId, userId);
	}
	
	/**
	 * 添加被关注
	 * @param userId
	 * @param currentUserId
	 */
	public void follower(String userId,String currentUserId){
		stringRedisTemplate.opsForSet().add("follower:"+userId, currentUserId);
	}
	
	/**
	 * 取消被关注
	 * @param userId
	 * @param currentUserId
	 */
	public void cancelFollower(String userId,String currentUserId){
		stringRedisTemplate.opsForSet().remove("follower:"+userId, currentUserId);
	}
	
	/**
	 * 获取关注的数量
	 * @param currentUserId
	 * @return
	 */
	public Long followingAmount(String currentUserId){
		return stringRedisTemplate.opsForSet().size("following:"+currentUserId);
	}
	/**
	 * 获取被关注的数量
	 * @param currentUserId
	 * @return
	 */
	public Long followerAmount(String currentUserId){
		return stringRedisTemplate.opsForSet().size("follower:"+currentUserId);
	}
	
	/**
	 * 获取关注的人的id
	 * @param currentUserId
	 * @return
	 */
	public List<String> followingUserId(String currentUserId){
		return new ArrayList<String>(stringRedisTemplate.opsForSet().members("following:"+currentUserId));
	}
	
}
